<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="1" formType="add" :labelWidth="90" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div v-if="printShow">
      <BasePrint ref="print" :data="printList" :isPreview="false" template="templateWorkOrder" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button type="primary" :loading="addBtnState" @click="openPrint">打 印</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import * as types from '@/api/common/auth/roleManage/types';
import { queryPrintData, queryTechnician, getDealerCompanyByCode } from '@/api/repair/repairAssign/repairAssign';
import { getAllUser } from '@/api/finance/advancesReceived/register';
import { getSfqfSelect } from '@/api/repair/repairReception/customer';
import moment from 'moment';

export default {
  name: 'printForm',
  mixins: [dictionary],
  props: ['roNo','modelName'],
  data() {
    this.saveData = null;
    return {
      printShow: false,
      formList: this.createFormList(),
      printList: {},
      allUser: [],
      sfqf: [],
      roStatus: []
    };
  },
  computed: {
    ...mapState('app', ['btnLoading']),
    addBtnState() {
      return this.btnLoading[types.YAN_ROLE_ADD];
    }
  },
  mounted() {
    this.getAllUser();
    this.getSfqfSelect();
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '技师',
          fieldName: 'id',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请输入技师名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否打印材料',
          fieldName: 'payment',
          initialValue: 10041001,
          itemList: this.createDictList('1004')
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async getAllUser() {
      const res = await getAllUser();
      this.allUser = res.data;
      this.queryTechnician();
    },
    async getSfqfSelect() {
      const res = await getSfqfSelect();
      this.sfqf = res.data;
    },
    async queryTechnician() {
      const res = await queryTechnician(this.roNo);
      let technician = res.data;
      for (const item of technician) {
        item.TECHNICIAN_NAME = this.allUser.find(x => x.userId == item.TECHNICIAN).employeeName;
      }
      technician.unshift({ TECHNICIAN: '-1', TECHNICIAN_NAME: '全部' });
      this.formList.find(x => x.fieldName === 'id').itemList = technician.map(x => ({ value: x.TECHNICIAN, text: x.TECHNICIAN_NAME }));
    },
    openPrint() {
      console.log(this.roNo,'主页面数据')
      this.$refs.formPanel.SUBMIT_FORM();
      console.log(this.formList, 'formList----');
      if (this.saveData != null) {
        let payment = this.formList.find(x => x.fieldName === 'payment').initialValue === 10041001 ? true : false;
        this.getPrintData(this.roNo, this.saveData.id, payment);
      } else {
        this.$notify.error({ title: '提示信息', message: '有必填项未填写!' });
      }
    },
    closeDialog() {
      this.$emit('close', false);
    },
    async getPrintData(roNo, id, payment) {
      this.printShow = true;
      const res = await queryPrintData(roNo, id);
      console.log(res, 'res-----------------');
      if (res.resultCode == 200) {
        this.printList = res.data;
        console.log(res.data,'接口返回');
        const res1 = await getDealerCompanyByCode();
        this.printList.companyName = res1.data.companyNameCN;
        this.printList.payment = payment;
        this.printList.repairOrderInfo.CREATED_AT = moment(this.printList.repairOrderInfo.CREATED_AT).format('YYYY-MM-DD HH:mm:ss');
        this.printList.repairOrderInfo.END_TIME_SUPPOSED = moment(this.printList.repairOrderInfo.END_TIME_SUPPOSED).format('YYYY-MM-DD HH:mm:ss');
        this.printList.repairOrderInfo.SERVICE_ADVISOR = this.allUser.find(x => x.userId == this.printList.repairOrderInfo.SERVICE_ADVISOR).employeeName;
        this.printList.repairOrderInfo.MODEL_NAME = this.modelName;
        for (const item of this.printList.repairLabour) {
          item.TECHNICIAN = this.allUser.find(x => x.userId == item.TECHNICIAN) ? this.allUser.find(x => x.userId == item.TECHNICIAN).employeeName : '';
          item.CHARGE_PARTITION_CODE = this.sfqf.find(x => x.CHARGE_PARTITION_CODE == item.CHARGE_PARTITION_CODE).CHARGE_PARTITION_NAME;
        }
        console.log(this.printList,'打印数据')
        this.$nextTick(() => {
          this.$refs.print.EXCUTE_PRINT();
        });
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
